<?php


namespace App\Library\Payment;


use App\Traits\GuzzleHttpTrait;
use Exception;
use Throwable;

class BasePayment
{
	use GuzzleHttpTrait;

	/**
	 * 接口返回码
	 */
	const CODE_SUCCESS = 200;    // 成功

	/**
	 * 自定义的：状态码
	 */
	const QUERY_PAY_STATUS_WAIT      = 0; // 待支付
	const QUERY_PAY_STATUS_SUCCESS   = 1; // 支付成功
	const QUERY_PAY_STATUS_CLOSED    = 2; // 关闭支付订单
	const QUERY_PAY_STATUS_NOT_FOUND = 3; // 支付订单不存在

	/**
	 * 获取地址
	 */
	protected function getDomain(): string
	{
		return '';
	}

	/**
	 * 加工url
	 *
	 * @param string $api
	 * @return string
	 */
	public function makeUrl(string $api): string
	{
		return $this->getDomain() . $api;
	}

	/**
	 * post 请求
	 *
	 * @param string $url
	 * @param array  $params
	 * @param bool   $origin
	 * @return array|mixed
	 * @throws Exception
	 */
	public function curlPost(string $url, array $params = [], bool $origin = false)
	{
		try {
			return $this->guzzleHttpPost($url, $params, $origin);
		} catch (Throwable $ex) {
			throw new Exception('接口返回失败:errMsg=' . $ex->getMessage());
		}
	}

	/**
	 * get 请求
	 *
	 * @param string $url
	 * @param array  $query
	 * @param bool   $origin
	 * @return array|mixed
	 * @throws Exception
	 */
	public function curlGet(string $url, array $query = [], bool $origin = false)
	{
		try {
			return $this->guzzleHttpGet($url, $query, $origin);
		} catch (Throwable $ex) {
			throw new Exception('接口返回失败:errMsg=' . $ex->getMessage());
		}
	}
}